package com.example.goodpet.activity.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.bumptech.glide.Glide;
import com.example.goodpet.R;
import com.example.goodpet.activity.UserInfoActivity;
import com.example.goodpet.bean.User;
import com.example.goodpet.bean.UserPetInfo;
import com.example.goodpet.home.homeRecommend.adapter.RecommendRecycleViewAdapter;
import com.example.goodpet.service.HomeService;
import com.example.goodpet.utils.Constants;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Retrofit;

public class ResultUserRecycleViewAdapter extends DelegateAdapter.Adapter<ResultUserRecycleViewAdapter.ViewHolder>{
    private static final String TAG = RecommendRecycleViewAdapter.class.getSimpleName();
    private final Context mContext;
    public List<User> datas;

    public ResultUserRecycleViewAdapter(Context mContext, List<User> users) {
        this.mContext=mContext;
        this.datas=users;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return null;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_result_user, parent, false);
        return new ResultUserRecycleViewAdapter.ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        User user = datas.get(position);
        Glide.with(mContext).
                load(Constants.BASE_URL_IMAGE+user.getPortrait())
                .into(holder.portrait);
        if (user.getGender()==0)
            holder.ivSex.setImageDrawable(mContext.getDrawable(R.drawable.man));
        else if (user.getGender()==1)
            holder.ivSex.setImageDrawable(mContext.getDrawable(R.drawable.women));
        holder.tvNickname.setText(user.getNickname());
        holder.tvSignature.setText(user.getSignature());

        holder.layoutEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserPetInfo userPetInfo = new UserPetInfo();
                userPetInfo.setUserId(user.getId());
                userPetInfo.setGender(user.getGender());
                userPetInfo.setNickname(user.getNickname());
                userPetInfo.setPhoneNumber(user.getPhoneNumber());
                userPetInfo.setPortrait(user.getPortrait());
                userPetInfo.setSignature(user.getSignature());
                Intent intent = new Intent(mContext, UserInfoActivity.class);
                intent.putExtra("user",userPetInfo);
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return datas==null?0:datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        CircleImageView portrait;
        ImageView ivSex;
        TextView tvNickname;
        TextView tvSignature;
        LinearLayout layoutEnter;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            portrait=itemView.findViewById(R.id.portrait);
            ivSex=itemView.findViewById(R.id.ivSex);
            tvNickname=itemView.findViewById(R.id.tvNickname);
            tvSignature=itemView.findViewById(R.id.tvSignature);
            layoutEnter=itemView.findViewById(R.id.layoutEnter);
        }
    }
}
